package FileInformation.helpers;

import FileInformation.utils.NumberParser;
import java.io.*;

/**
 * Created by IntelliJ IDEA.
 * User: Nobody
 * Date: 25.04.11
 * Time: 21:24
 * To change this template use File | Settings | File Templates.
 */
public class ColumnCounter extends AbstractFileHelper implements Serializable {

    private int firstDataRowNumber;
    private int columns;
    private NumberParser numberParser;

    public int getColumns() {
        return columns;
    }

    public void setFirstDataRowNumber(int firstDataRowNumber) {
        this.firstDataRowNumber = firstDataRowNumber;
    }

    public ColumnCounter(File file, NumberParser numberParser) {
        super(file);
        this.numberParser = numberParser;
    }

    @Override
    public void execute() throws IOException {
        FileInputStream fstream = new FileInputStream(file);
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        for (int i = 0; i < firstDataRowNumber - 1; ++i) {
            br.readLine();
        }
        String line = br.readLine();
        if (line == null) {
            throw new IOException("file " + file + " is empty.");
        }
        columns = numberParser.countNumbersPresentInCharArray(line);
        br.close();
    }
}
